package com.scalar.chiptrack.sales;

import com.scalar.chiptrack.sales.dao.SalesOrderDAO;
import com.scalar.chiptrack.sales.dao.NewSalesOrderDAO;
import com.scalar.chiptrack.utils.ConfigManager;
import com.scalar.chiptrack.utils.StringUtils;
import com.scalar.chiptrack.utils.Mailer;
import com.scalar.chiptrack.users.dao.UserDAO;
import com.scalar.chiptrack.commons.Labels;
import com.scalar.chiptrack.commons.Constants;
import com.scalar.ScalarException;
import org.apache.struts.util.MessageResources;

import java.io.File;

/**
 * Created by IntelliJ IDEA.
 * User: Krishna Prasad
 * Date: Dec 6, 2006
 * Time: 6:56:19 PM
 * To change this template use File | Settings | File Templates.
 */
public class SalesOrderHelper
{

    public static void notifyFinanceReasonCode ( int salesOrderId )
    {
        SalesOrder salesOrder   = null;
        String from             = null;
        String to               = null;
        String cc               = null;
        String subject          = null;
        String message          = null;

        try
        {
            salesOrder = SalesOrderDAO.loadSalesOrder ( salesOrderId + "" );

            if ( SalesOrderDAO.isSalesOrderApprovedButFinanceReasonCode ( salesOrder.getSalesOrderId () ) == true )
            {
                // Construct receipient list
                //from = "Chiptrac Admin <" + ConfigManager.getSysAdminEmail () + ">";
                from =  ConfigManager.getFromEmailId(Constants.FROM_EMAIL_SALES_GROUP_NAME );
                //to = salesOrder.getEnteredByName () + "<" + UserDAO.getEmailByUserId ( salesOrder.getEnteredById () ) + ">";
                to = salesOrder.getEnteredByName () + "<" + UserDAO.loadEmailByInternalUserId ( null, salesOrder.getEnteredById () ) + ">";
                cc = StringUtils.concatStrings ( SalesOrderDAO.loadEmailListForApprovedSalesOrderNotification (), Mailer.EMAIL_SEPARATOR );

                //Send Notification mail to Customer Contact Person also
                /*if( salesOrder.getEmail() != null && salesOrder.getEmail().trim().length() > 0 )
                {
                    if( cc == null || cc.trim().length() == 0 )
                    {
                        cc = salesOrder.getEmail();
                    }
                    else
                    {
                        cc = cc + "," + salesOrder.getEmail();
                    }
                }*/
                String method = salesOrder.getShippingMethodName();
                String shippingMethod  = null ;
                if(method != null && method.trim().length()!= 0)
                {
                     shippingMethod = salesOrder.getShippingMethodName() +"#"+ salesOrder.getShippingAccount();
                }
                else
                {
                    shippingMethod = "";
                }
                String shippingMethodLabel  = null;
                String endUserLabel         = null;
                String endCustomerLabel     = null;
                String representativeLabel  = null;
                String heading              = null;
                String deliveryTermsLabel   = null;
                try
                {
                    shippingMethodLabel = MessageResources.getMessageResources ( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage ( "label.sales.salesOrder.ShippingMethod" );
                    if ( shippingMethodLabel == null )
                    {
                        shippingMethodLabel = Labels.DEFAULT_SHIPPING_METHOD_LABEL;
                    }
                }
                catch ( NullPointerException e )
                {
                    shippingMethodLabel    = Labels.DEFAULT_SHIPPING_METHOD_LABEL;
                }

                try
                {
                    endCustomerLabel = MessageResources.getMessageResources ( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage ( "label.sales.endCustomer" );
                    if ( endCustomerLabel == null )
                    {
                        endCustomerLabel = Labels.DEFAULT_END_CUSTOMER_LABEL;
                    }
                }
                catch ( NullPointerException e )
                {
                    endCustomerLabel = Labels.DEFAULT_END_CUSTOMER_LABEL;
                }
                try
                {
                    endUserLabel = MessageResources.getMessageResources ( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage ( "label.sales.endUser" );
                    if ( endUserLabel == null )
                    {
                        endUserLabel = Labels.DEFAULT_END_USER_LABEL;
                    }
                }
                catch ( NullPointerException e )
                {
                    endUserLabel = Labels.DEFAULT_END_USER_LABEL;
                }

                try
                {
                    representativeLabel = MessageResources.getMessageResources ( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage ( "label.sales.salesRep.Name" );
                    if ( representativeLabel == null )
                    {
                        representativeLabel = Labels.DEFAULT_REPRESENTATIVE;
                    }
                }
                catch ( NullPointerException e )
                {
                    representativeLabel = Labels.DEFAULT_REPRESENTATIVE;
                }

                try
                {
                    deliveryTermsLabel = MessageResources.getMessageResources ( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage ( "label.sales.salesOrder.DeliveryTerms" );
                    if ( deliveryTermsLabel == null )
                    {
                        deliveryTermsLabel = Labels.DEFAULT_DELIVERY_TERMS_LABEL;
                    }
                }
                catch ( NullPointerException e )
                {
                    deliveryTermsLabel    = Labels.DEFAULT_DELIVERY_TERMS_LABEL;
                }

                if ( salesOrder.getIsSample() )
                {
                    subject = "All the holds except finance have been removed on the Sample Order " + salesOrder.getSalesOrderNo () + "";
                    heading = "sample";
                }
                else
                {
                    subject = "All the holds except finance have been removed on the Sales Order " + salesOrder.getSalesOrderNo () + "";
                    heading = "sales";
                }

                message = "<HTML><BODY><P style='FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Verdana'>This is to notify that all the holds except for the finance have been removed on the "+heading+" order <b>" + salesOrder.getSalesOrderNo () + "</b> . <BR><BR><TABLE WIDTH=677 BORDER=0 CELLPADDING=4 CELLSPACING=0 style='FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Verdana'><TR BGCOLOR=\"#ccccff\"><TH COLSPAN=5 WIDTH=669 HEIGHT=20 VALIGN=CENTER><P ALIGN=LEFT>"+heading.replaceFirst("s","S")+" Order Information:</P></TH></TR>" +
                        "<TR BGCOLOR=\"#e6e6e6\"><TD WIDTH=136 VALIGN=TOP><P>Sale Order No:</P></TD><TD WIDTH=167 VALIGN=BOTTOM SDVAL=\"10185\" SDNUM=\"1033;\"><P ALIGN=LEFT>" +
                        salesOrder.getSalesOrderNo () + "</P></TD><TD WIDTH=33 VALIGN=TOP><P><BR></P></TD><TD WIDTH=134 VALIGN=TOP><P>Customer Name:</P></TD><TD WIDTH=167 VALIGN=TOP><P>" +
                        salesOrder.getCustomerName () + "</P></TD></TR><TR BGCOLOR=\"#e6e6e6\" VALIGN=TOP><TD WIDTH=136><P ALIGN=LEFT>Customer PO:</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                        salesOrder.getCustomerPO () + "</P></TD><TD WIDTH=33><P ALIGN=LEFT><BR></P></TD><TD WIDTH=134><P ALIGN=LEFT>Contact Person:</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                        salesOrder.getContactPerson () + "</P></TD></TR><TR BGCOLOR=\"#e6e6e6\" VALIGN=TOP><TD WIDTH=136><P ALIGN=LEFT>Phone No:</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                        salesOrder.getPhoneNumber() + "</P></TD><TD WIDTH=33><P ALIGN=LEFT><BR></P></TD><TD WIDTH=134><P ALIGN=LEFT>Cell No:</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                        salesOrder.getCellNumber() +"</P></TD></TR><TR BGCOLOR=\"#e6e6e6\" VALIGN=TOP><TD WIDTH=136><P ALIGN=LEFT>Fax :</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                        salesOrder.getFaxNumber() + "</P></TD><TD WIDTH=33><P ALIGN=LEFT><BR></P></TD><TD WIDTH=134><P ALIGN=LEFT>Email:</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                        salesOrder.getEmail() +"</P></TD></TR><TR BGCOLOR=\"#e6e6e6\" VALIGN=TOP><TD WIDTH=136><P ALIGN=LEFT>Sales Person:</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                        salesOrder.getSalesPerson() + "</P></TD><TD WIDTH=33><P ALIGN=LEFT><BR></P></TD><TD WIDTH=134><P ALIGN=LEFT>"+representativeLabel+" :</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                        salesOrder.getSalesRep() +	"</P></TD></TR><TR BGCOLOR=\"#e6e6e6\" VALIGN=TOP><TD WIDTH=136><P ALIGN=LEFT>"+shippingMethodLabel+" :</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                        shippingMethod+"</P></TD><TD WIDTH=33><P ALIGN=LEFT><BR></P></TD><TD WIDTH=134><P ALIGN=LEFT></P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                        "" +"</P></TD></TR><TR BGCOLOR=\"#e6e6e6\" VALIGN=TOP><TD WIDTH=136><P ALIGN=LEFT>Pay Terms :</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                        salesOrder.getShippingTerms() + "</P></TD><TD WIDTH=33><P ALIGN=LEFT><BR></P></TD><TD WIDTH=134><P ALIGN=LEFT>"+deliveryTermsLabel+" :</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                        salesOrder.getDeliveryTerms() +	"</P></TD></TR><TR BGCOLOR=\"#e6e6e6\" VALIGN=TOP><TD WIDTH=136><P ALIGN=LEFT>Regional Rep :</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                        salesOrder.getRegionalRep() + "</P></TD><TD WIDTH=33><P ALIGN=LEFT><BR></P></TD><TD WIDTH=134><P ALIGN=LEFT>Booking Date:</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                        salesOrder.getSalesOrderDate() +"</P></TD></TR><TR BGCOLOR=\"#e6e6e6\" VALIGN=TOP><TD WIDTH=136><P ALIGN=LEFT>"+endUserLabel+" :</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                        salesOrder.getEndCustomer() + "</P></TD><TD WIDTH=33><P ALIGN=LEFT><BR></P></TD><TD WIDTH=134><P ALIGN=LEFT>"+endCustomerLabel+" :</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                        salesOrder.getOdm() + "</P></TD></TR><TR BGCOLOR=\"#e6e6e6\" VALIGN=TOP><TD WIDTH=136><P ALIGN=LEFT>Billing Address :</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                        salesOrder.getBillingAddress() + "</P></TD><TD WIDTH=33><P ALIGN=LEFT><BR></P></TD><TD WIDTH=134><P ALIGN=LEFT>Shipping Address:</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                        salesOrder.getShippingAddress() +"</P></TD></TR><TR BGCOLOR=\"#e6e6e6\" VALIGN=TOP><TD WIDTH=136><P ALIGN=LEFT>Freigh Instructions :</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                        salesOrder.getFreightInstructions() + "</P></TD><TD WIDTH=33><P ALIGN=LEFT><BR></P></TD><TD WIDTH=134><P ALIGN=LEFT>Shipping Instructions :</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                        salesOrder.getShippingInstructions() +"</P></TD></TR><TR BGCOLOR=\"#e6e6e6\" VALIGN=TOP><TD WIDTH=136><P ALIGN=LEFT>Comments :</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                        salesOrder.getComments() + 	"</P></TD><TD WIDTH=33><P ALIGN=LEFT><BR></P></TD><TD WIDTH=134><P ALIGN=LEFT></P></TD><TD WIDTH=167><P ALIGN=LEFT></TD></TR></TABLE></BODY></HTML>";


                Mailer.send ( from,
                              to,
                              cc,
                              null /*bcc*/,
                              subject,
                              message,
                              null /*attachments*/,
                              Mailer.CONTENT_TYPE_HTML,
                              false /*Dont verify if the receipient is a ChipTrac user*/ );

            }
        }
        catch ( Exception e )
        {
            e.printStackTrace ();
            System.out.println ( "WARNING!! Could not notify " + to + " about the finance reason code for the  sales order: " + salesOrder.getSalesOrderNo () );
        }
    }

    // Notifies the SO Originator etc about the approved sales order
    public static void notifyApprovedSalesOrderInfo ( int salesOrderId )
    {
        SalesOrder salesOrder   = null;
        String from             = null;
        String to               = null;
        String cc               = null;
        String subject          = null;
        /*String message          = null;

        String shippingMethod       = null ;
        String shippingMethodLabel  = null;
        String endUserLabel         = null;
        String endCustomerLabel     = null;
        String representativeLabel  = null;
        String deliveryTermsLabel   = null;
        String heading              = null;
        String method               = null;*/
        
        String customerNotificatonEmail = null;


        try
        {
            if ( SalesOrderDAO.isSalesOrderApproved ( salesOrderId ) )
            {
                salesOrder = SalesOrderDAO.loadSalesOrder( salesOrderId + "" );

                // Construct receipient list
                //from = "Chiptrac Admin <" + ConfigManager.getSysAdminEmail() + ">";
                from =  ConfigManager.getFromEmailId( Constants.FROM_EMAIL_SALES_GROUP_NAME );

                to = UserDAO.loadEmailByInternalUserId( null, salesOrder.getEnteredById() );

                if( to != null && to.trim().length() > 0 )
                {
                    to = salesOrder.getEnteredByName() + "<" + to + ">" ;
                }

                if ( ConfigManager.isSendApprovedSalesOrderNotificationMailToCustomer() )
                {
                    customerNotificatonEmail = StringUtils.concatStrings ( NewSalesOrderDAO.loadCustomerNotificationsEmails( salesOrder.getCustomerId(), "SalesOrder" ), Mailer.EMAIL_SEPARATOR );
                    if( customerNotificatonEmail != null && customerNotificatonEmail.trim().length() > 0 )
                    {
                        if( cc == null || cc.trim().length() == 0 )
                        {
                            cc = customerNotificatonEmail;
                        }
                        else
                        {
                            cc = cc + "," + customerNotificatonEmail;
                        }
                    }
                }

                if( ( to == null || to.trim().length() == 0 ) && ( cc != null && cc.trim().length() > 0 ) )
                {
                    to = cc;
                    cc = "";
                }

                //Send Notification mail to Customer Contact Person also
                /*if( salesOrder.getEmail() != null && salesOrder.getEmail().trim().length() > 0 )
                {
                    if( cc == null || cc.trim().length() == 0 )
                    {
                        cc = salesOrder.getEmail();
                    }
                    else
                    {
                        cc = cc + "," + salesOrder.getEmail();
                    }
                }*/
                /*method = salesOrder.getShippingMethodName();

                if(method != null && method.trim().length()!= 0)
                {
                     shippingMethod = salesOrder.getShippingMethodName() +"#"+ salesOrder.getShippingAccount();
                }
                else
                {
                    shippingMethod = "";
                }

                try
                {
                    shippingMethodLabel = MessageResources.getMessageResources ( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage ( "label.sales.salesOrder.ShippingMethod" );
                    if ( shippingMethodLabel == null )
                    {
                        shippingMethodLabel = Labels.DEFAULT_SHIPPING_METHOD_LABEL;
                    }
                }
                catch ( NullPointerException e )
                {
                    shippingMethodLabel    = Labels.DEFAULT_SHIPPING_METHOD_LABEL;
                }

                try
                {
                    endCustomerLabel = MessageResources.getMessageResources ( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage ( "label.sales.endCustomer" );
                    if ( endCustomerLabel == null )
                    {
                        endCustomerLabel = Labels.DEFAULT_END_CUSTOMER_LABEL;
                    }
                }
                catch ( NullPointerException e )
                {
                    endCustomerLabel = Labels.DEFAULT_END_CUSTOMER_LABEL;
                }

                try
                {
                    endUserLabel = MessageResources.getMessageResources ( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage ( "label.sales.endUser" );
                    if ( endUserLabel == null )
                    {
                        endUserLabel = Labels.DEFAULT_END_USER_LABEL;
                    }
                }
                catch ( NullPointerException e )
                {
                    endUserLabel = Labels.DEFAULT_END_USER_LABEL;
                }


                try
                {
                    representativeLabel = MessageResources.getMessageResources ( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage ( "label.sales.salesRep.Name" );
                    if ( representativeLabel == null )
                    {
                        representativeLabel = Labels.DEFAULT_REPRESENTATIVE;
                    }
                }
                catch ( NullPointerException e )
                {
                    representativeLabel = Labels.DEFAULT_REPRESENTATIVE;
                }
                try
                {
                    deliveryTermsLabel = MessageResources.getMessageResources ( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage ( "label.sales.salesOrder.DeliveryTerms" );
                    if ( deliveryTermsLabel == null )
                    {
                        deliveryTermsLabel = Labels.DEFAULT_DELIVERY_TERMS_LABEL;
                    }
                }
                catch ( NullPointerException e )
                {
                    deliveryTermsLabel    = Labels.DEFAULT_DELIVERY_TERMS_LABEL;
                }*/
                if ( salesOrder.getIsSample() )
                {
                    subject = "Sample Order " + salesOrder.getSalesOrderNo() + " has been approved";
                    //heading = "sample";
                }
                else
                {
                    subject = "Sales Order " + salesOrder.getSalesOrderNo() + " has been approved";
                    //heading = "sales";
                }
                /*message = "<HTML><BODY><P style='FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Verdana'>This is to notify that the "+heading+" order <b>" + salesOrder.getSalesOrderNo() + "</b> has been approved. <BR><BR><TABLE WIDTH=677 BORDER=0 CELLPADDING=4 CELLSPACING=0 style='FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Verdana'><TR BGCOLOR=\"#ccccff\"><TH COLSPAN=5 WIDTH=669 HEIGHT=20 VALIGN=CENTER><P ALIGN=LEFT>"+heading.replaceFirst("s","S")+" Order Information:</P></TH></TR>" +
                            "<TR BGCOLOR=\"#e6e6e6\"><TD WIDTH=136 VALIGN=TOP><P style='FONT-SIZE: 8pt; COLOR: black; FONT-FAMILY: Verdana'>Sale Order No:</P></TD><TD WIDTH=167 VALIGN=BOTTOM SDVAL=\"10185\" SDNUM=\"1033;\"><P ALIGN=LEFT>" +
                            salesOrder.getSalesOrderNo() + "</P></TD><TD WIDTH=33 VALIGN=TOP><P><BR></P></TD><TD WIDTH=134 VALIGN=TOP><P>Customer Name:</P></TD><TD WIDTH=167 VALIGN=TOP><P>" +
                            salesOrder.getCustomerName() + "</P></TD></TR><TR BGCOLOR=\"#e6e6e6\" VALIGN=TOP><TD WIDTH=136><P ALIGN=LEFT>Customer PO:</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                            salesOrder.getCustomerPO() + "</P></TD><TD WIDTH=33><P ALIGN=LEFT><BR></P></TD><TD WIDTH=134><P ALIGN=LEFT>Contact Person:</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                            salesOrder.getContactPerson() +	"</P></TD></TR><TR BGCOLOR=\"#e6e6e6\" VALIGN=TOP><TD WIDTH=136><P ALIGN=LEFT>Phone No:</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                            salesOrder.getPhoneNumber() + "</P></TD><TD WIDTH=33><P ALIGN=LEFT><BR></P></TD><TD WIDTH=134><P ALIGN=LEFT>Cell No:</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                            salesOrder.getCellNumber() +"</P></TD></TR><TR BGCOLOR=\"#e6e6e6\" VALIGN=TOP><TD WIDTH=136><P ALIGN=LEFT>Fax :</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                            salesOrder.getFaxNumber() + "</P></TD><TD WIDTH=33><P ALIGN=LEFT><BR></P></TD><TD WIDTH=134><P ALIGN=LEFT>Email:</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                            salesOrder.getEmail() +"</P></TD></TR><TR BGCOLOR=\"#e6e6e6\" VALIGN=TOP><TD WIDTH=136><P ALIGN=LEFT>Sales Person:</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                            salesOrder.getSalesPerson() + "</P></TD><TD WIDTH=33><P ALIGN=LEFT><BR></P></TD><TD WIDTH=134><P ALIGN=LEFT>"+representativeLabel+" :</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                            salesOrder.getSalesRep() +	"</P></TD></TR><TR BGCOLOR=\"#e6e6e6\" VALIGN=TOP><TD WIDTH=136><P ALIGN=LEFT>"+shippingMethodLabel+" :</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                            shippingMethod+"</P></TD><TD WIDTH=33><P ALIGN=LEFT><BR></P></TD><TD WIDTH=134><P ALIGN=LEFT></P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                            "" +"</P></TD></TR><TR BGCOLOR=\"#e6e6e6\" VALIGN=TOP><TD WIDTH=136><P ALIGN=LEFT>Pay Terms :</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                            salesOrder.getShippingTerms() + "</P></TD><TD WIDTH=33><P ALIGN=LEFT><BR></P></TD><TD WIDTH=134><P ALIGN=LEFT>"+deliveryTermsLabel+" :</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                            salesOrder.getDeliveryTerms() +	"</P></TD></TR><TR BGCOLOR=\"#e6e6e6\" VALIGN=TOP><TD WIDTH=136><P ALIGN=LEFT>Regional Rep :</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                            salesOrder.getRegionalRep() + "</P></TD><TD WIDTH=33><P ALIGN=LEFT><BR></P></TD><TD WIDTH=134><P ALIGN=LEFT>Booking Date:</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                            salesOrder.getSalesOrderDate() +"</P></TD></TR><TR BGCOLOR=\"#e6e6e6\" VALIGN=TOP><TD WIDTH=136><P ALIGN=LEFT>"+endUserLabel+" :</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                            salesOrder.getEndCustomer() + "</P></TD><TD WIDTH=33><P ALIGN=LEFT><BR></P></TD><TD WIDTH=134><P ALIGN=LEFT>"+endCustomerLabel+" :</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                            salesOrder.getOdm() + "</P></TD></TR><TR BGCOLOR=\"#e6e6e6\" VALIGN=TOP><TD WIDTH=136><P ALIGN=LEFT>Billing Address :</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                            salesOrder.getBillingAddress() + "</P></TD><TD WIDTH=33><P ALIGN=LEFT><BR></P></TD><TD WIDTH=134><P ALIGN=LEFT>Shipping Address:</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                            salesOrder.getShippingAddress() +"</P></TD></TR><TR BGCOLOR=\"#e6e6e6\" VALIGN=TOP><TD WIDTH=136><P ALIGN=LEFT>Freigh Instructions :</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                            salesOrder.getFreightInstructions() + "</P></TD><TD WIDTH=33><P ALIGN=LEFT><BR></P></TD><TD WIDTH=134><P ALIGN=LEFT>Shipping Instructions :</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                            salesOrder.getShippingInstructions() +"</P></TD></TR><TR BGCOLOR=\"#e6e6e6\" VALIGN=TOP><TD WIDTH=136><P ALIGN=LEFT>Comments :</P></TD><TD WIDTH=167><P ALIGN=LEFT>" +
                            salesOrder.getComments() + 	"</P></TD><TD WIDTH=33><P ALIGN=LEFT><BR></P></TD><TD WIDTH=134><P ALIGN=LEFT></P></TD><TD WIDTH=167><P ALIGN=LEFT></TD></TR></TABLE></BODY></HTML>";

*/
                if( to != null && to.trim().length() > 0 )
                {
                    Mailer.send( from,
                                 to,
                                 cc,
                                 null /*bcc*/,
                                 subject,
                                 salesOrder.getHtmlViewWithChangeSummary( "", false ),//message,
                                 null /*attachments*/,
                                 Mailer.CONTENT_TYPE_HTML,
                                 false /*Dont verify if the receipient is a ChipTrac user*/ );
                }
            }
        }
        catch ( Exception e )
        {
            e.printStackTrace ();
            System.out.println ( "WARNING!! Could not notify " + to + " about the approved sales order: " + salesOrder.getSalesOrderNo () );
        }
    }

    public static String getFileAttachmentName( String attachmentFileLocation ) throws ScalarException 
    {
        String tempPhysicalFileName = "";
		File fileListDir = new File ( attachmentFileLocation );
        if( fileListDir != null && fileListDir.exists() )
        {
            File[] fileList = fileListDir.listFiles();
            if( fileList != null && fileList.length > 0 )
            {
                for( int i = 0; i < fileList.length; i++ )
                {
                    if( fileList[i].isFile() )
                    {
                        tempPhysicalFileName = fileList[i].getName();
                        break;
                    }
                }
            }
        }
        return tempPhysicalFileName;
    }

}
